# @Remark: crm

import time
from selenium.webdriver.common.action_chains import ActionChains

from Common.basepage import BasePage
from PageLocators.login.login_page_locator import LoginPageLocator as loc
from PageLocators.CRM.crm_home_loc import CrmHomePageLocator as crm_loc
from PageLocators.CRM.crm_home_loc import CrmClassTemplateLocator as cct_loc
from PageLocators.CRM.crm_home_loc import CrmOrderRecordLocator as cor_loc
from PageLocators.CRM.crm_home_loc import CrmUserAddLocator as cua_loc
from PageLocators.CRM.crm_home_loc import ClassManagementLocator as cml_loc
from PageLocators.CRM.crm_home_loc import CrmQuitClassLocator as  cqcl_loc


class CrmHomePage(BasePage):
    # 打开导航栏
    def open_navigation(self):
        self.click_element(crm_loc.navigation_btn_loc, "打开CRM首页导航栏")
        time.sleep(1)

    # crm首页-学生服务
    def go_stu(self):
        self.click_element(crm_loc.list_stu_loc, "跳转学生服务")

    # crm导航- 班级管理
    def go_class(self):
        self.click_element(crm_loc.list_class_loc, "班级管理")
        time.sleep(5)

    # crm导航- 订单管理
    def go_order(self):
        self.click_element(crm_loc.list_order_loc, "订单管理")
        time.sleep(5)

    # 获取登录失败提示信息
    def get_errorMsg(self):
        return self.get_element_text(loc.login_error_loc, "登录失败错误提示！")

    def assert_welcome(self):
        return self.get_element(loc.home_welcome, img_doc='登录完成_获取首页元素')


class ClassManagementPage(BasePage):
    """班级管理页面"""
    def go_temp(self):
        """去模板管理"""
        self.click_element(cct_loc.list_template_loc, "点击模板管理导航")
        time.sleep(3)

    def create_temp(self, course_name="UI创建课程"):
        """新建模版"""
        self.click_element(cct_loc.create_temp_btn_loc, "点击新建模板按钮")
        time.sleep(3)
        self.click_element(cct_loc.add_loc, "点击添加课程")
        self.input_text(cct_loc.course_name_loc, "课程名称输入框", course_name)
        self.click_element(cct_loc.search_loc, "点击搜索")
        time.sleep(2)
        cct_loc.checkbox_loc_lst[1] = cct_loc.checkbox_loc_lst[1].format(course_name)
        checkbox_loc = tuple(cct_loc.checkbox_loc_lst)
        self.click_element(checkbox_loc, "点击勾选按钮")
        time.sleep(2)
        self.click_element(cct_loc.confirm_loc, "创建模版-选择班级-确定按钮")
        time.sleep(5)

    def change_class_roll_to_bottom(self, x=0, y=10000):
        """选择课程弹层滑动"""
        js = "document.getElementsByClassName('mat-dialog-container ng-tns-c68-77 ng-trigger ng-trigger-dialogContainer " \
             "ng-star-inserted')[0].scrollTo(0,10000)"
        js = "document.getElementsByClassName('td-layout-manage-list-content')[0].scrollTo({x}, {y})".format(x=x, y=y)
        self.driver.execute_script(js)
        time.sleep(1)
        # 确定
        self.click_element(cct_loc.confirm_loc, "确认选择课程")
        time.sleep(1)

    def channel_info(self):
        """渠道信息"""
        self.click_element(cct_loc.class_channel_loc, "点击渠道")
        self.input_text(cct_loc.channel_input_loc, "检索渠道", "测试")
        time.sleep(1)
        self.click_element(cct_loc.test_channel_loc, "选择测试渠道")
        time.sleep(1)

    def class_platform(self, num=12):
        """上课平台"""
        self.input_text(cct_loc.class_capacity_loc, "直播班容", num)

    def free_price_info(self, num=0):
        """免费课时数"""
        self.input_text(cct_loc.free_class_hours_loc, "免费课时数", num)

    def class_time_setting(self, num=2):
        """班级上课时间设置"""
        self.input_text(cct_loc.class_number_loc, "开班量", num)

    def lesson_price_info(self, price=123, display_price=234, contract_price=345):
        """价格信息"""
        self.input_text(cct_loc.price_loc, "售价", price)
        self.input_text(cct_loc.display_price, "显示价", display_price)
        self.input_text(cct_loc.contract_price, "合同价", contract_price)
        # self.click_element(cct_loc.temp_detail_submit_confirm_loc, "创建模版--确定")  # 暂时注释不创建

    def temp_scroll(self, x=0, y=500):
        """班级模板详情-滑动"""
        js = "document.getElementsByClassName('td-layout-manage-list-content')[0].scrollTo({x}, {y})".format(x=x, y=y)
        self.driver.execute_script(js)
        time.sleep(1)

    def submit(self):
        self.click_element(cct_loc.submit_confirm_loc, "创建模版确定按钮")
        time.sleep(3)

    def put_class(self,):
        text = ""
        try:
            self.click_element(cml_loc.reset_loc, "重置")
            time.sleep(2)
            self.click_element(cml_loc.search_loc, "搜索")
            time.sleep(5)
            self.click_element(cml_loc.checkbox_loc, "勾选")
            self.click_element(cml_loc.put_loc, "上架")
            time.sleep(1)
            self.click_element(cml_loc.confirm_loc, "确定")
            text = self.get_element_text(cml_loc.class_id_loc, "列表班级编号")
            print("班级编号：{}".format(text))
        except:
            print("上架班级失败")
        time.sleep(3)
        return text

    def get_temp_id(self):
        self.go_temp()
        time.sleep(3)
        self.click_element(cct_loc.temp_search_loc, "搜索模版")
        time.sleep(3)
        try:
            temp_id = self.get_element_text(cct_loc.td_temp_id_loc, "获取列表第一列模版id")
            temp_name = self.get_element_text(cct_loc.td_temp_name_loc, "获取列表第一列模版id")
            data = {"temp_id": temp_id, "temp_name": temp_name}
        except:
            data = None
        return data


class CrmOrderPage(BasePage):
    """学员下单流程"""
    def go_user_add(self):
        self.click_element(crm_loc.navigation_btn_loc, "打开导航栏")
        time.sleep(0.5)
        self.click_element(crm_loc.list_user_add_loc, "用户添加导航")
        time.sleep(5)

    def go_foreign_user(self):
        """国外用户查询导航"""
        self.click_element(cua_loc.list_foreign_user_query_loc, "国外用户查询")
        time.sleep(1)

    def go_domestic_user(self):
        """（用户添加）国外用户查询导航"""
        self.click_element(cua_loc.list_domestic_user_add_loc, "用户添加（国内）")
        time.sleep(1)

    def search_foreign_user(self, mobile):
        """国外用户搜索"""
        self.input_text(cua_loc.user_input_loc, "手机号收入框", mobile)
        self.click_element(cua_loc.btn_search_loc, "点击搜索")
        time.sleep(3)

    def search_foreign_mail_user(self, email):
        """国外用户搜索--按照邮箱账号"""
        self.click_element(cqcl_loc.mobile_select_loc, "点击手机号下拉框")
        self.click_element(cqcl_loc.mail_lco, "选择邮箱")
        self.input_text(cqcl_loc.mail_input_loc, "邮箱输入框", email)
        self.click_element(cua_loc.btn_search_loc, "点击搜索")
        time.sleep(3)

    def search_domestic_user(self, mobile, **kwargs):
        """国内用户搜索"""
        self.input_text(cua_loc.domestic_user_input_loc, "国内手机号收入框", mobile)
        self.click_element(cua_loc.btn_search_loc, "点击搜索")
        time.sleep(3)
        create_user_title = self.get_create_user_title()
        if create_user_title:
            self.create_domestic_user(**kwargs)
            self.add_student(**kwargs)
        time.sleep(1)

    def domestic_place_an_order(self, class_id, receipt_data, order_description):
        """国内学员下单"""
        self.click_element(cua_loc.btn_PlaceAnOrder_loc, "下单")
        self.select_class(class_id)
        self.no_pay(receipt_data)
        self.order_detail_scroll()
        self.order_detail(order_description)

    def create_domestic_user(self, **kwargs):
        """创建国内用户
        channel_1_name: 获客渠道一
        channel_2_name：获客渠道二
        parent_name：家长名
        relationship： 与孩子关系
        province_name：省名
        city_name：市名
        county_name：区县
        """
        cua_loc.channel_1_name_loc[1] = cua_loc.channel_1_name_loc[1].format(kwargs['channel_1_name'])
        channel_1_name_loc = tuple(cua_loc.channel_1_name_loc)
        self.drop_down_not_select(cua_loc.obtain_channel_1_loc, channel_1_name_loc, "获客渠道一")
        cua_loc.channel_2_name_loc[1] = cua_loc.channel_2_name_loc[1].format(kwargs['channel_2_name'])
        channel_2_name_loc = tuple(cua_loc.channel_2_name_loc)
        self.drop_down_not_select(cua_loc.obtain_channel_2_loc, channel_2_name_loc, "获客渠道二")
        self.input_text(cua_loc.parent_name_loc, "家长姓名", kwargs['parent_name'])
        cua_loc.rwc_fatherOrmother_loc[1] = cua_loc.rwc_fatherOrmother_loc[1].format(kwargs['relationship'])
        rwc_fatherOrmother_loc = tuple(cua_loc.rwc_fatherOrmother_loc)
        self.drop_down_not_select(cua_loc.rwc_loc, rwc_fatherOrmother_loc, "与孩子关系")
        cua_loc.province_name_loc[1] = cua_loc.province_name_loc[1].format(kwargs['province_name'])
        cua_loc.city_name_loc[1] = cua_loc.city_name_loc[1].format(kwargs['city_name'])
        cua_loc.county_name_loc[1] = cua_loc.county_name_loc[1].format(kwargs['county_name'])
        province_name_loc = tuple(cua_loc.province_name_loc)
        city_name_loc = tuple(cua_loc.city_name_loc)
        county_name_loc = tuple(cua_loc.county_name_loc)
        self.drop_down_not_select(cua_loc.province_loc, province_name_loc, "省")
        self.drop_down_not_select(cua_loc.city_loc, city_name_loc, "市")
        self.drop_down_not_select(cua_loc.county_loc, county_name_loc, "区县")
        self.click_element(cua_loc.created_loc, "创建客户")
        time.sleep(2)

    def add_student(self, **kwargs):
        """创建国内用户--添加学员
        student_name: 学员姓名
        student_English_name： 学员英文名
        sex：性别
        age：年龄
        """
        self.click_element(cua_loc.add_student_loc, "添加学员")
        self.input_text(cua_loc.student_name_loc, "学员中文名", kwargs['student_name'])
        self.input_text(cua_loc.student_English_name_loc, "学员英文名名", kwargs['student_English_name'])
        self.student_scroll(y=50)
        cua_loc.sex_loc[1] = cua_loc.sex_loc[1].format(kwargs['sex'])
        sex_loc = tuple(cua_loc.sex_loc)
        self.drop_down_not_select(cua_loc.gender_loc, sex_loc, "选择性别")
        cua_loc.age_loc[1] = cua_loc.age_loc[1].format(kwargs['age'])
        age_loc = tuple(cua_loc.age_loc)
        self.drop_down_not_select(cua_loc.age_select_loc, age_loc, "选择年龄")
        self.student_scroll(y=500)
        self.click_element(cua_loc.determine_loc, "添加学员-确定")

    def student_scroll(self, x=0, y=100):
        js = "document.getElementsByClassName('cdk-overlay-pane')[0].firstElementChild.nextElementSibling.scrollTo({x},{y})".format(x=x, y=y)
        self.driver.execute_script(js)

    def get_create_user_title(self):
        """没有查询到用户时会弹出创建用户弹层，获取弹层title"""
        # for i in range(10):
        #     time.sleep(0.5)
        try:
            text = self.get_element_text(cua_loc.create_user_loc, "创建用户title")
            # if text:
                # break
        except:
            text = ""
        return text

    def order_record(self):
        """国外用户下单"""
        self.click_element(cua_loc.btn_PlaceAnOrder_loc, "下单")
        time.sleep(3)

    def select_class(self, class_id):
        """选择班级弹窗页"""
        time.sleep(2)
        self.input_text(cua_loc.input_class_loc, "输入班级id", class_id)
        time.sleep(1)
        self.click_element(cua_loc.mat_add_loc, "添加班级加号")
        self.click_element(cua_loc.btn_confirm_loc, "确定按钮")
        time.sleep(3)

    def no_pay(self, receipt_data):
        self.click_element(cua_loc.no_pay_loc, "勾选免支付")
        self.input_text(cua_loc.receipt_loc, "填写回执单", receipt_data)

    def order_detail_scroll(self, x=0, y=1000):
        js = "document.getElementsByClassName('td-layout-manage-list-content')[0].scrollTo({x}, {y})".format(x=x, y=y)
        self.driver.execute_script(js)

    def order_detail(self, order_description):
        time.sleep(2)
        actions = ActionChains(self.driver)
        self.click_element(cua_loc.order_description_loc, "订单描述")
        actions.send_keys(order_description).perform()
        self.click_element(cua_loc.btn_submit_loc, "订单提交")
        time.sleep(10)

    def submit_assert(self):
        try:
            text = self.get_element_text(cua_loc.wait_div_loc, "断言信息")
            return text
        except:
            return ""


class CrmOrderReviewPage(BasePage):
    """订单审核"""
    def query_order(self, mobile):
        """查询订单"""
        self.click_element(cor_loc.list_OrderRecord_loc, "订单记录导航")
        time.sleep(1)
        self.drop_down_not_select(cor_loc.audit_status_loc, cor_loc.await_audit_loc, "审核状态-待审核")
        self.input_text(cor_loc.parent_mobile_loc, "输入家长手机号", mobile)
        self.click_element(cor_loc.all_order_loc1, "显示全部订单")
        self.click_element(cor_loc.order_search_loc, "订单搜索")
        time.sleep(2)
        cor_loc.order_id_loc[1] = cor_loc.order_id_loc[1].format(mobile=mobile)
        order_id_loc = tuple(cor_loc.order_id_loc)
        order_id = self.get_element_text(order_id_loc, "获取订单号")
        return order_id

    def order_review(self, order_id):
        """订单审核"""
        self.click_element(cor_loc.list_OrderReview_loc, "订单审核导航")
        self.click_element(cor_loc.all_order_loc, "勾选显示全部订单")
        self.input_text(cor_loc.input_OrderId_loc, "输入订单编号", order_id)
        self.click_element(cor_loc.order_search_loc, "订单搜索")
        print('order_id,', order_id)
        time.sleep(10)
        cor_loc.OrderToCheckbox_loc[1] = cor_loc.OrderToCheckbox_loc[1].format(orderId=order_id)
        checkbox_loc = tuple(cor_loc.OrderToCheckbox_loc)
        print('checkbox_loc,', checkbox_loc)
        for i in range(30):
            time.sleep(0.5)
            try:
                self.click_element(checkbox_loc, "通过订单号勾选")
                break
            except:
                continue
        self.click_element(cor_loc.btn_pass_loc, "批量通过按钮")
        for i in range(30):
            time.sleep(0.5)
            toast = self.toast_wait_eleVisible(cor_loc.toast_loc, "提示")
            if toast:
                break
        return toast


class CrmTempCreateClassPage(BasePage):
    def search_temp(self, temp_id):
        self.input_text(cct_loc.temp_id_loc, "输入模版ID", temp_id)
        self.click_element(cct_loc.temp_search_loc, "搜索模版")
        time.sleep(2)

    def create_class(self):
        self.click_element(cct_loc.table_edit_loc, "点击编辑")
        time.sleep(2)
        js = "document.getElementsByClassName('td-layout-manage-list-content')[0].scrollTo(0,1500)"
        self.driver.execute_script(js)
        self.input_text(cct_loc.amount_of_class_loc, "开班量", "1")
        self.click_element(cct_loc.create_class_loc, "开班")
        time.sleep(5)

    def put_class(self, temp_id):
        text = ""
        try:
            self.click_element(cml_loc.reset_loc, "重置")
            self.drop_down_not_select(cml_loc.select_subject_loc, cml_loc.subject_loc, "中文科目")
            time.sleep(2)
            self.drop_down_not_select(cml_loc.select_condition_loc, cml_loc.temp_id_loc, "选择模版ID")
            time.sleep(1)
            self.input_text(cml_loc.input_temp_id_loc, "输入模版ID", temp_id)
            self.click_element(cml_loc.search_loc, "搜索")
            time.sleep(5)
            self.click_element(cml_loc.checkbox_loc, "勾选")
            self.click_element(cml_loc.put_loc, "上架")
            time.sleep(1)
            self.click_element(cml_loc.confirm_loc, "确定")
            text = self.get_element_text(cml_loc.class_id_loc, "列表班级编号")
            print("班级编号：{}".format(text))
        except:
            print("上架班级失败")
        return text


class CrmQuitClassPage(BasePage):
    """退班流程"""
    def quit_class(self):
        """国内退班"""
        js = "document.getElementsByClassName('td-layout-manage-list-content')[0].scrollTo(0,2500)"
        self.driver.execute_script(js)
        time.sleep(1)
        self.click_element(cqcl_loc.class_info_loc, "点击班级信息")
        self.click_element(cqcl_loc.quit_class_loc, "点击退班按钮")
        time.sleep(1)
        try:
            self.click_element(cqcl_loc.lesson_checkbox_layout, "勾选退班课时")
        except:
            pass
        # 滑动弹窗
        self.driver.execute_script(cqcl_loc.js_loc)
        # self.drop_down_not_select(cqcl_loc.textbook_status_loc, cqcl_loc.no_send_loc, "选择教材寄回状态-未寄出")
        self.quit_class_remarks("退班备注123")

    def foreign_quit_class(self, classId):
        """国外退班"""
        js = "document.getElementsByClassName('td-layout-manage-list-content')[0].scrollTo(0,2500)"
        self.driver.execute_script(js)
        time.sleep(1)
        self.click_element(cqcl_loc.class_info_loc, "点击班级信息")
        self.click_foreign_quit(classId)
        self.click_element(cqcl_loc.foreign_checkbox_loc, "勾选待退课时")
        # 滑动弹窗
        self.driver.execute_script(cqcl_loc.js_loc)
        self.quit_class_remarks("退班备注123")

    def click_foreign_quit(self, classId):
        """国外点击退班， 通过班级id"""
        cqcl_loc.class_id_to_quit_loc[1] = cqcl_loc.class_id_to_quit_loc[1].format(classId)
        class_id_to_quit_loc = tuple(cqcl_loc.class_id_to_quit_loc)
        self.click_element(class_id_to_quit_loc, "点击退班")
        time.sleep(2)

    def quit_class_remarks(self, remarks):
        time.sleep(1)
        actions = ActionChains(self.driver)
        self.click_element(cqcl_loc.remarks_loc, "退班备注")
        actions.send_keys(remarks).perform()
        self.click_element(cqcl_loc.confirm_loc, "确认按钮")
        time.sleep(3)

    def quit_class_review(self, mobile):
        """退班审核"""
        self.click_element(cqcl_loc.quit_class_review, "退班审核导航")
        time.sleep(3)
        self.click_element(cqcl_loc.dis_all_loc, "勾选显示全部班级")
        self.input_text(cqcl_loc.parent_mobile_loc, "输入家长手机号", mobile)
        time.sleep(3)
        cqcl_loc.checkbox_loc[1] = cqcl_loc.checkbox_loc[1].format(mobile)
        checkbox_loc = tuple(cqcl_loc.checkbox_loc)
        self.click_element(checkbox_loc, "勾选列表退班数据")
        self.click_element(cqcl_loc.pass_loc, "批量审核通过")

    def foreign_quit_class_review(self, class_id):
        """国外退班审核"""
        self.click_element(cqcl_loc.quit_class_review, "退班审核导航")
        time.sleep(3)
        self.click_element(cqcl_loc.dis_all_loc, "勾选显示全部班级")
        self.click_element(cqcl_loc.is_special_channel_loc, "是否特殊渠道")
        self.click_element(cqcl_loc.all_channel_loc, "是否特殊渠道-所有")
        self.input_text(cqcl_loc.quit_class_id_input_loc, "班级编号", class_id)
        time.sleep(3)
        self.click_element(cqcl_loc.foreign_quit_checkbox_loc, "勾选列表退班数据")
        self.click_element(cqcl_loc.pass_loc, "批量审核通过")

    def review_assert(self):
        # try:
        #     text = self.get_element_text(cqcl_loc.toast_loc, "断言信息")
        #     return text
        # except:
        #     return ""
        toast = ""
        for i in range(30):
            time.sleep(0.5)
            toast = self.toast_wait_eleVisible(cor_loc.toast_loc, "提示")
            if toast:
                break
        return toast